﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ReportSqlFormater {
    /// <summary>
    /// Lógica de interacción para MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
        }


        private String formatSql(String str,char concatenador) {
            String[] SqlParticionado;
            SqlParticionado = particionaSql(str, 50);
            StringBuilder result= new StringBuilder();
            foreach (String aux in SqlParticionado) {
                result.Append(" SQL= SQL " + concatenador +" \""+ aux + "\"\n"); 
            }
            return result.ToString();
        }

        private String[] particionaSql(String str,int largo) {
            List<String> sqlPart= new List<string>();
            int position = 0;
            while (position<str.Length){
                int aux = 0;
                try {
                    while (true) {
                        if (!(position + largo + aux == str.Length)) {
                            if (str[position + largo + aux] == ' ') {
                                sqlPart.Add(str.Substring(position, largo + aux));
                                break;
                            }
                        } else {
                            sqlPart.Add(str.Substring(position, largo + aux));
                        }
                            aux++;
                    }
                } catch (Exception e) {
                    return sqlPart.ToArray();
                }
             position = position + largo + aux;
            }
            return sqlPart.ToArray();
        }

        private void TextBlock_LostFocus(object sender, RoutedEventArgs e) {
            if(fuente.Text != "")
            result.Text = formatSql(fuente.Text, '&');
        }

        private void Button_Click(object sender, RoutedEventArgs e) {
            this.Close();
        }
    }
}
